package in1

import (
	"behind/models"
	"behind/pkg/app"
	"behind/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetIn1(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchText := ""
	if arg := c.Query("searchText"); arg != "" {
		searchText = arg
	}
	in1Param := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchText": searchText,
	}
	err, info, total := models.GetAllIn1(in1Param)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func AddIn1(c *gin.Context) {
	var in1data models.In1
	c.ShouldBind(&in1data)
	err, info := models.AddIn1(in1data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "插入成功")
}

func UpadateIn1(c *gin.Context) {
	var in1data models.In1
	c.ShouldBind(&in1data)
	err, info := models.UpdateIn1(in1data.Inno1, in1data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "修改成功")
}

func DeleteIn1(c *gin.Context) {
	var in1data models.In1
	c.ShouldBind(&in1data)
	err, info := models.DeleteIn1(in1data.Inno1)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "删除成功")
}
